﻿package com.tdzx.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.CoreConnectionPNames;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.os.Handler;

import com.baidu.platform.comapi.basestruct.GeoPoint;
import com.tdzx.constant.HttpConfigAPI;
import com.tdzx.constant.MainConstants;
import com.tdzx.entity.CircleCommet;
import com.tdzx.entity.CircleInfoS;
import com.tdzx.entity.CityArea;
import com.tdzx.entity.CityArea.Circle;
import com.tdzx.entity.Comment;
import com.tdzx.entity.KuanterService;
import com.tdzx.entity.KuanterService.Service;
import com.tdzx.entity.Member;
import com.tdzx.entity.MerchanDetail;
import com.tdzx.entity.SiteInfo;
import com.tdzx.entity.TuanDTO;

public class JsonParser {

	//获取网点信息
	public static Map getSites(String json) {
		Map map = new HashMap();
		List<SiteInfo> sites = null;
		try {
			JSONObject object = new JSONObject(json);
			if(StringUtils.isSuccessTD(json)) {
				int sum = object.optInt("maxResult");
				map.put("sum", sum);
				
				JSONArray array = object.optJSONArray("resutObject");
				if(array != null && array.length() > 0) {
					sites = new ArrayList<SiteInfo>();
					for(int i = 0; i < array.length(); i++) {
						SiteInfo info = new SiteInfo();
						JSONObject jo = array.optJSONObject(i);
						info.setCommentCount(jo.optString("commentCount"));
						info.setDistance(jo.optString("distance"));
						info.setGoods_address(jo.optString("goods_address"));
						info.setGoods_areacode(jo.optString("goods_areacode"));
						info.setGoods_Desc(jo.optString("goods_Desc"));
						info.setGoods_id(jo.optInt("goods_id"));
						info.setGoods_image(jo.optString("goods_image"));
						info.setGoods_Isgroup(jo.optString("goods_Isgroup"));
						info.setGoods_Name(jo.optString("goods_Name"));
						info.setGoods_telephone(jo.optString("goods_telephone"));
						info.setLabelName(jo.optString("labelName"));
						info.setScore(jo.optDouble("score"));
						info.setUser_Id(jo.optString("user_Id"));
						info.setVveragePrice(jo.optDouble("vveragePrice"));
						Double longtitude = jo.optDouble("goods_Jingdu");
						Double latitude = jo.optDouble("goods_WeiDu");
						GeoPoint point = new GeoPoint((int)(latitude * 1E6), (int)(longtitude * 1E6));
						info.setPoint(point);
						sites.add(info);
					}
					map.put("siteList", sites);
				}
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
		
		
		return map;
	}
	
	public static List<KuanterService> getKuanterService(String json) {
		List<KuanterService> list_all = new ArrayList<KuanterService>();
		try {
			JSONObject object = new JSONObject(json);
			if(StringUtils.isSuccess(json)) {
				JSONArray array = object.optJSONArray("serviceCategoryList");
				if(array != null && array.length() > 0) {
					for(int i = 0; i < array.length(); i++) {
						KuanterService kuanterService = new KuanterService();
						JSONObject kuanterJson = array.optJSONObject(i);
						kuanterService.setServiceID(kuanterJson.optString("id"));
						kuanterService.setServiceType(kuanterJson.optString("categoryName"));
						kuanterService.setSeq(kuanterJson.optString("seq"));
						kuanterService.setParentId(kuanterJson.optString("parentId"));
						
						JSONArray array_service = kuanterJson.optJSONArray("subCategory");
						if(array_service != null && array_service.length() > 0) {
							List<Service> list_service = new ArrayList<KuanterService.Service>();
							for(int j = 0; j < array_service.length(); j++) {
								JSONObject service_json = array_service.optJSONObject(j);
								Service service = new Service();
								service.setServiceID(service_json.optString("id"));
								service.setServiceName(service_json.optString("categoryName"));
								service.setSeq(service_json.optString("seq"));
								service.setParentId(service_json.optString("parentId"));
								service.setSubCategory(service_json.optString("subCategory"));
								list_service.add(service);
							}
							kuanterService.setList_service(list_service);
						}
						if(!kuanterService.getServiceID().equals(MainConstants.QT)) {
							list_all.add(kuanterService);
						}
//						list_all.add(kuanterService);
					}
				}
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
		
		return list_all;
	}
	
	public static List<CityArea> getArea(String json,Handler handler) {
		List<CityArea> list_area = null;
		try {
			JSONObject object = new JSONObject(json);
			if(StringUtils.isSuccess(json)) {
				JSONArray ja_area = object.optJSONArray("areaList");
				if(ja_area != null && ja_area.length() > 0) {
					list_area = new ArrayList<CityArea>();
					for(int i = 0; i < ja_area.length(); i++) {
						CityArea area = new CityArea();
						JSONObject jo = ja_area.optJSONObject(i);
						area.setAreaID(jo.optString("areaCode"));
						area.setCityarea(jo.optString("areaName"));
						area.setGpsCoordinate(jo.optString("gpsCoordinate"));
						area.setStandardCoordinate(jo.optString("standardCoordinate"));
						list_area.add(area);
					}
				}
				for(int i = 0; i < list_area.size(); i++) {
					CityArea area = list_area.get(i);
					new Thread(new GetAreaInfo(area.getAreaID(), handler, area)).start();
				}
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
		
		return list_area;
	}
	
	
	static class GetAreaInfo implements Runnable {
		String supAreaCode;
		Handler handler;
		CityArea ca;
		public GetAreaInfo(String supAreaCode,Handler handler,CityArea ca) {
			this.supAreaCode = supAreaCode;
			this.handler = handler;
			this.ca = ca;
		}

		@Override
		public void run() {
			List<NameValuePair> params = new ArrayList<NameValuePair>();
			params.add(new BasicNameValuePair("supAreaCode",supAreaCode));
			
			HttpClient client = new DefaultHttpClient();
			HttpPost post = new HttpPost(MainConstants.HOST_test + HttpConfigAPI.FINDAREA);
			HttpResponse respons = null;
			String result = "";
			try {
				if (params != null) {
					UrlEncodedFormEntity entity = new UrlEncodedFormEntity(
							params, HTTP.UTF_8);
					post.setEntity(entity);
				}

				client.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 30000);	//连接超时
				client.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 30000);   //读取超时
				respons = client.execute(post);

				if (respons.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
					result = EntityUtils.toString(respons.getEntity());
					List<Circle> list_cilcle = null;
					JSONObject object = new JSONObject(result);
					if(StringUtils.isSuccess(result)) {
						list_cilcle = new ArrayList<Circle>();
						Circle all = new Circle();
						all.setCircleID(ca.getAreaID());
						all.setCircleName(ca.getCityarea() + "全境");
						all.setGpsCoordinate(ca.getGpsCoordinate());
						all.setStandardCoordinate(ca.getStandardCoordinate());
						list_cilcle.add(all);
						JSONArray ja_area = object.optJSONArray("areaList");
						if(ja_area != null && ja_area.length() > 0) {
							for(int i = 0; i < ja_area.length(); i++) {
								Circle circle = new Circle();
								JSONObject jo = ja_area.optJSONObject(i);
								circle.setCircleID(jo.optString("areaCode"));
								circle.setCircleName(jo.optString("areaName"));
								circle.setGpsCoordinate(jo.optString("gpsCoordinate"));
								circle.setStandardCoordinate(jo.optString("standardCoordinate"));
								list_cilcle.add(circle);
							}
						}
						ca.setCircleList(list_cilcle);
					}
				}
			} catch (Exception e) {
				e.printStackTrace();
			} 
		}
		
	}
	
	public static Member getMember(String json) {
		Member member = null;
		try {
			JSONObject jo = new JSONObject(json);
			String status = jo.optString("status");
			if(status.equals("200")) {
				String isguanzhu = jo.optString("isguanzhu");
				JSONObject jo_user = jo.optJSONObject("resutObject");
				if(jo_user != null) {
					member = new Member();
					member.setIsguanzhu(isguanzhu);
					member.setMember_Address(jo_user.optString("member_Address"));
					member.setMember_Avatar(jo_user.optString("member_Avatar"));
					member.setMember_Id(jo_user.optString("member_Id"));
					member.setMember_Nick_Name(jo_user.optString("member_Nick_Name"));
					member.setMember_Phone(jo_user.optString("member_Phone"));
					member.setMemeber_Sex(jo_user.optString("member_Sex"));
					member.setMember_Info(jo_user.optString("member_info"));
					member.setLat(jo_user.optString("lat"));
					member.setLon(jo_user.optString("lon"));
					member.setDistance(jo_user.optString("distance"));
				}
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
		
		return member;
	}
	
	public static List<Member> getMemberList(String json) {
		List<Member> list = null;
		try {
			JSONObject jo = new JSONObject(json);
			String status = jo.optString("status");
			if (status.equals("200")) {
				JSONArray ja_user = jo.optJSONArray("resutObject");
				if (ja_user != null) {
					list = new ArrayList<Member>();
					for (int i = 0; i < ja_user.length(); i++) {
						JSONObject jo_user = ja_user.optJSONObject(i);
						Member member = new Member();
						member.setMember_Address(jo_user
								.optString("member_Address"));
						member.setMember_Avatar(jo_user
								.optString("member_Avatar"));
						member.setMember_Id(jo_user.optString("member_Id"));
						member.setMember_Nick_Name(jo_user
								.optString("member_Nick_Name"));
						member.setMember_Phone(jo_user
								.optString("member_Phone"));
						member.setMemeber_Sex(jo_user.optString("member_Sex"));
						
						member.setMember_Info(jo_user.optString("member_info"));
						member.setLat(jo_user.optString("lat"));
						member.setLon(jo_user.optString("lon"));
						member.setDistance(jo_user.optString("distance"));
						list.add(member);
					}
				}
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}

		return list;
	}
	
	public static Map getTuanList(String json) {
		Map map = new HashMap();
		List<TuanDTO> sites = null;
		try {
			JSONObject object = new JSONObject(json);
			if(StringUtils.isSuccessTD(json)) {
				int sum = object.optInt("maxResult");
				map.put("sum", sum);
				
				JSONArray array = object.optJSONArray("resutObject");
				if(array != null && array.length() > 0) {
					sites = new ArrayList<TuanDTO>();
					for(int i = 0; i < array.length(); i++) {
						TuanDTO info = new TuanDTO();
						JSONObject jo = array.optJSONObject(i);
						info.setBuyPeople(jo.optString("buyPeople"));
						info.setDistance(jo.optString("distance"));
						info.setGoods_id(jo.optString("goods_id"));
						info.setGoods_name(jo.optString("goodsName"));
						info.setGroup_context(jo.optString("group_context"));
						info.setGroup_endtime(jo.optLong("group_endtime"));
						info.setGroup_id(jo.optString("group_id"));
						info.setGroup_imag(jo.optString("group_imag"));
						info.setGroup_info(jo.optString("group_info"));
						info.setGroup_name(jo.optString("group_name"));
						info.setGroup_price(jo.optDouble("group_price"));
						info.setGroup_starttime(jo.optLong("group_starttime"));
						info.setGroup_tuangouprice(jo.optDouble("group_tuangouprice"));
						sites.add(info);
					}
					map.put("siteList", sites);
				}
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
		
		
		return map;
	}
	
	
	public static List<TuanDTO> getListTuanSc(String json) {
		List<TuanDTO> sites = null;
		try {
			JSONArray array = new JSONArray(json);
			if(array != null && array.length() > 0) {
				sites = new ArrayList<TuanDTO>();
				for(int i = 0; i < array.length(); i++) {
					TuanDTO info = new TuanDTO();
					JSONObject jo = array.optJSONObject(i);
					info.setBuyPeople(jo.optString("buyPeople"));
					info.setDistance(jo.optString("distance"));
					info.setGoods_id(jo.optString("goods_id"));
					info.setGoods_name(jo.optString("goods_name"));
					info.setGroup_context(jo.optString("group_context"));
					info.setGroup_endtime(jo.optLong("group_endtime"));
					info.setGroup_id(jo.optString("group_id"));
					info.setGroup_imag(jo.optString("group_imag"));
					info.setGroup_info(jo.optString("group_info"));
					info.setGroup_name(jo.optString("group_name"));
					info.setGroup_price(jo.optDouble("group_price"));
					info.setGroup_starttime(jo.optLong("group_starttime"));
					info.setGroup_tuangouprice(jo.optDouble("group_tuangouprice"));
					sites.add(info);
				}
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return sites;
	}
	
	public static List<TuanDTO> getListTuanSearch(String json) {
		List<TuanDTO> sites = null;
		try {
			JSONObject all = new JSONObject(json);
			JSONArray array = all.optJSONArray("resutObject");
			if(array != null && array.length() > 0) {
				sites = new ArrayList<TuanDTO>();
				for(int i = 0; i < array.length(); i++) {
					TuanDTO info = new TuanDTO();
					JSONObject jo = array.optJSONObject(i);
					info.setBuyPeople(jo.optString("buyPeople"));
					info.setDistance(jo.optString("distance"));
					info.setGoods_id(jo.optString("goods_id"));
					info.setGoods_name(jo.optString("goods_name"));
					info.setGroup_context(jo.optString("group_context"));
					info.setGroup_endtime(jo.optLong("group_endtime"));
					info.setGroup_id(jo.optString("group_id"));
					info.setGroup_imag(jo.optString("group_imag"));
					info.setGroup_info(jo.optString("group_info"));
					info.setGroup_name(jo.optString("group_name"));
					info.setGroup_price(jo.optDouble("group_price"));
					info.setGroup_starttime(jo.optLong("group_starttime"));
					info.setGroup_tuangouprice(jo.optDouble("group_tuangouprice"));
					sites.add(info);
				}
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return sites;
	}
	
	public static List<SiteInfo> getListMerchanSc(String json) {
		List<SiteInfo> sites = null;
		try {
			JSONArray array = new JSONArray(json);
			if(array != null && array.length() > 0) {
				sites = new ArrayList<SiteInfo>();
				for(int i = 0; i < array.length(); i++) {
					SiteInfo info = new SiteInfo();
					JSONObject jo = array.optJSONObject(i);
					info.setCommentCount(jo.optString("commentCount"));
					info.setDistance(jo.optString("distance"));
					info.setGoods_address(jo.optString("goods_address"));
					info.setGoods_areacode(jo.optString("goods_areacode"));
					info.setGoods_Desc(jo.optString("goods_Desc"));
					info.setGoods_id(jo.optInt("goods_id"));
					info.setGoods_image(jo.optString("goods_image"));
					info.setGoods_Isgroup(jo.optString("goods_Isgroup"));
					info.setGoods_Name(jo.optString("goods_Name"));
					info.setGoods_telephone(jo.optString("goods_telephone"));
					info.setLabelName(jo.optString("labelName"));
					info.setScore(jo.optDouble("score"));
					info.setUser_Id(jo.optString("user_Id"));
					info.setVveragePrice(jo.optDouble("vveragePrice"));
					Double longtitude = jo.optDouble("goods_Jingdu");
					Double latitude = jo.optDouble("goods_WeiDu");
					GeoPoint point = new GeoPoint((int)(latitude * 1E6), (int)(longtitude * 1E6));
					info.setPoint(point);
					sites.add(info);
				}
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return sites;
	}
	
	public static List<SiteInfo> getListMerchanSearch(String json) {
		List<SiteInfo> sites = null;
		try {
			JSONObject jo_all = new JSONObject(json);
			JSONArray array = jo_all.optJSONArray("resutObject");
			if(array != null && array.length() > 0) {
				sites = new ArrayList<SiteInfo>();
				for(int i = 0; i < array.length(); i++) {
					SiteInfo info = new SiteInfo();
					JSONObject jo = array.optJSONObject(i);
					info.setCommentCount(jo.optString("commentCount"));
					info.setDistance(jo.optString("distance"));
					info.setGoods_address(jo.optString("goods_address"));
					info.setGoods_areacode(jo.optString("goods_areacode"));
					info.setGoods_Desc(jo.optString("goods_Desc"));
					info.setGoods_id(jo.optInt("goods_id"));
					info.setGoods_image(jo.optString("goods_image"));
					info.setGoods_Isgroup(jo.optString("goods_Isgroup"));
					info.setGoods_Name(jo.optString("goods_Name"));
					info.setGoods_telephone(jo.optString("goods_telephone"));
					info.setLabelName(jo.optString("labelName"));
					info.setScore(jo.optDouble("score"));
					info.setUser_Id(jo.optString("user_Id"));
					info.setVveragePrice(jo.optDouble("vveragePrice"));
					Double longtitude = jo.optDouble("goods_Jingdu");
					Double latitude = jo.optDouble("goods_WeiDu");
					GeoPoint point = new GeoPoint((int)(latitude * 1E6), (int)(longtitude * 1E6));
					info.setPoint(point);
					sites.add(info);
				}
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return sites;
	}
	
	public static MerchanDetail getMerchanDetails(String json) {
		MerchanDetail merchanDetail = null;
		try {
			if(json == null) return merchanDetail;
			JSONObject jo = new JSONObject(json);
			if(StringUtils.isSuccessTD(json)) {
				JSONObject jo_all = jo.optJSONObject("resutObject");
				if(jo_all != null) {
					merchanDetail = new MerchanDetail();
					JSONObject jo_commet = jo_all.optJSONObject("commentEntity");
					if(jo_commet != null) {
						Comment commet = new Comment();
						commet.setComment_Date(jo_commet.optLong("comment_Date"));
						commet.setComment_Id(jo_commet.optString("comment_Id"));
						commet.setGoods_Id(jo_commet.optString("goods_Id"));
						commet.setComment_code(jo_commet.optDouble("comment_code"));
						commet.setComment_price(jo_commet.optDouble("comment_price"));
						commet.setComment_Content(jo_commet.optString("comment_Content"));
						commet.setMemberName(jo_commet.optString("memberName"));
						commet.setMemberimage(jo_commet.optString("memberimage"));
						merchanDetail.setCommet(commet);
					}
					
					SiteInfo siteInfo = new SiteInfo();
					JSONObject jo_site = jo_all.optJSONObject("goodsEntity");
					siteInfo.setGoods_address(jo_site.optString("goods_address"));
					siteInfo.setGoods_Desc(jo_site.optString("goods_Desc"));
					siteInfo.setGoods_image(jo_site.optString("goods_image"));
					siteInfo.setGoods_Name(jo_site.optString("goods_Name"));
					siteInfo.setGoods_telephone(jo_site.optString("goods_telephone"));
					siteInfo.setScore(jo_site.optDouble("score"));
					siteInfo.setVveragePrice(jo_site.optDouble("vveragePrice"));
					Double longtitude = jo_site.optDouble("goods_WeiDu");
					Double latitude = jo_site.optDouble("goods_Jingdu");
					siteInfo.setGoods_id(jo_site.optInt("goods_id"));
					GeoPoint point = new GeoPoint((int)(longtitude * 1E6), (int)(latitude * 1E6));
					siteInfo.setPoint(point);
					merchanDetail.setSiteInfo(siteInfo);
				}
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return merchanDetail;
	}
	
	//获取网点信息
		public static Map getSites_1(String json) {
			Map map = new HashMap();
			List<SiteInfo> sites = null;
			try {
				JSONObject object = new JSONObject(json);
				if(StringUtils.isSuccessTD(json)) {
					int sum = object.optInt("maxResult");
					map.put("sum", sum);
					
					JSONArray array = object.optJSONArray("resutObject");
					if(array != null && array.length() > 0) {
						sites = new ArrayList<SiteInfo>();
						for(int i = 0; i < array.length(); i++) {
							SiteInfo info = new SiteInfo();
							JSONObject jo = array.optJSONObject(i);
							info.setCommentCount(jo.optString("commentCount"));
							info.setDistance(jo.optString("distance"));
							info.setGoods_address(jo.optString("goods_address"));
							info.setGoods_areacode(jo.optString("goods_areacode"));
							info.setGoods_Desc(jo.optString("goods_Desc"));
							info.setGoods_id(jo.optInt("goods_id"));
							info.setGoods_image(jo.optString("goods_image"));
							info.setGoods_Isgroup(jo.optString("goods_Isgroup"));
							info.setGoods_Name(jo.optString("goods_name"));
							info.setGoods_telephone(jo.optString("goods_telephone"));
							info.setLabelName(jo.optString("labelName"));
							info.setScore(jo.optDouble("score"));
							info.setUser_Id(jo.optString("user_Id"));
							info.setVveragePrice(jo.optDouble("vveragePrice"));
							Double longtitude = jo.optDouble("goods_Jingdu");
							Double latitude = jo.optDouble("goods_WeiDu");
							GeoPoint point = new GeoPoint((int)(latitude * 1E6), (int)(longtitude * 1E6));
							info.setPoint(point);
							sites.add(info);
						}
						map.put("siteList", sites);
					}
				}
			} catch (JSONException e) {
				e.printStackTrace();
			}
			
			
			return map;
		}
		
		public static CircleInfoS getCircleInfo(String json) {
			CircleInfoS circleInfo = new CircleInfoS();
			try {
				JSONObject jo = new JSONObject(json);
				if(StringUtils.isSuccessTD(json)) {
					JSONObject resutObject = jo.optJSONObject("resutObject");
					if(resutObject != null) {
						circleInfo.setCommentcount(resutObject.optString("commentcount"));
						JSONArray ja = resutObject.optJSONArray("listNewCirlImage");
						if(ja != null) {
							List<String> list = new ArrayList<String>();
							for(int i = 0; i < ja.length(); i++) {
								JSONObject jo_img = ja.optJSONObject(i);
								String url = jo_img.optString("newcirl_image");
								list.add(url);
							}
							circleInfo.setImageList(list);
						}
						
						JSONArray ja_c = resutObject.optJSONArray("listComment");
						if(ja_c != null) {
							List<CircleCommet> list_c = new ArrayList<CircleCommet>();
							for(int i = 0; i < ja_c.length(); i++) {
								JSONObject jo_c = ja_c.optJSONObject(i);
								CircleCommet cc = new CircleCommet();
								cc.setuDate(jo_c.optLong("comment_Date"));
								cc.setuImg(jo_c.optString("memberimage"));
								cc.setuName(jo_c.optString("memberName"));
								cc.setuText(jo_c.optString("comment_Content"));
								list_c.add(cc);
							}
							circleInfo.setCommetList(list_c);
						}
					}
				}
			} catch (JSONException e) {
				e.printStackTrace();
			}
			
			return circleInfo;
		}
}
